package com.buildings.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.bean.TaskBean;
import com.buildings.bean.TaskNewBean;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;


/**
 * 任务列表
 */

public class TaskListNewAdapter extends RecyclerView.Adapter<TaskListNewAdapter.ViewHolder> {

    private List<TaskNewBean> data;
    private Activity activity;

    public TaskListNewAdapter(Activity activity, List<TaskNewBean> data) {
        this.data = data;
        this.activity = activity;
    }

    public void update(List<TaskNewBean> data) {
        this.data = data;
        this.notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_task_list, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int i) {
        TaskNewBean bean = data.get(i);
        holder.tvName.setText(bean.getTaskName());
        //修改任务
        holder.btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onTask(v,i);
            }
        });
        //执行任务
        holder.btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onForm(v,i);
            }
        });
        //查看表单
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(v,i);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView btnSubmit;//修改任务
        TextView btnEdit;//执行任务
        TextView btnDelete;//查看表单
        TextView tvName;//查看表单

        public ViewHolder(View itemView) {
            super(itemView);
            btnSubmit = itemView.findViewById(R.id.btn_submit);
            btnEdit = itemView.findViewById(R.id.btn_edit);
            btnDelete = itemView.findViewById(R.id.btn_delete);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onTask(View view, int position);

        void onForm(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}